package com.mvc.dao.Impl;

import com.mvc.bean.Student;
import com.mvc.dao.StudentDao;
import com.mvc.utils.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl extends DruidUtil implements StudentDao {
    Connection  connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;

    /**
     * 查询所有的学生信息
     * @return
     */
    @Override
    public List<Student> findAll() {
        List<Student> list = new ArrayList<>();
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement("select * from student");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setStuname(resultSet.getString("stuname"));
                student.setAge(resultSet.getInt("age"));
                student.setSex(resultSet.getInt("sex"));
                list.add(student);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(connection,preparedStatement,resultSet);
        }
        return list;
    }
}
